package com.letv.business.flow.play;

import com.ledim.utils.LogUtil;
import com.letv.ads.AdsManager;
import com.letv.android.client.LetvSDK;
import com.letv.component.player.core.LetvMediaPlayerManager;
import com.letv.component.player.utils.NativeInfos;
import com.letv.component.player.videoview.VideoViewH264m3u8;
import com.letv.core.config.LetvConfig;
import java.net.URLEncoder;

import android.content.Context;
import android.text.TextUtils;

import com.letv.ads.ADPlayFragment;
import com.letv.business.flow.album.AlbumPlayBaseFlow;
import com.letv.business.flow.album.model.AlbumPlayInfo;
import com.letv.core.constant.PlayConstant;
import com.letv.core.db.PreferencesManager;
import com.letv.core.utils.LetvSDKUtils;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.StringUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.DataStatistics;
import com.letv.datastatistics.entity.StatisticsPlayInfo;
import com.letv.datastatistics.util.DataConstant;
import com.letv.datastatistics.util.DataUtils;

import rx.internal.operators.CompletableOnSubscribeTimeout;

public class PlayStatisticsUtils {

    /**
     * 播放统计新增launch
     */
    public static void staticsLauncher(Context context, String aid, String vid, String cid, int launchMode,
            String uuid, int ipt) {
        if (!TextUtils.isEmpty("aid") && (aid.equals("0") || aid.equals(""))) {
            aid = "-";
        }
        if (!TextUtils.isEmpty("vid") && (vid.equals("0") || vid.equals("") || vid.equals("-1"))) {
            vid = "-";
        }
        if (!TextUtils.isEmpty(cid) && (cid.equals("0") || cid.equals(""))) {
            cid = "-";
        }
        StatisticsPlayInfo playInfo = new StatisticsPlayInfo();
        playInfo.setcTime(System.currentTimeMillis());
        playInfo.setIpt(ipt);

        switch (launchMode) {
            case PlayConstant.LAUNCH_MODE_LIVE_MUSIC:
            case PlayConstant.LAUNCH_MODE_LIVE_LUNBO:
                DataStatistics.getInstance().sendLivePlayInfo25New(context, "0", "0",
                        DataConstant.StaticticsVersion2Constatnt.PlayerAction.LAUNCHER_ACTION, "0", "0", "-",
                        LetvUtils.getUID(), uuid, "-", aid, vid, "-", "-", "2", null, null, null, null, null, "-", LetvConfig
                                .getPcode(context),
                        PreferencesManager.getInstance().isLogin() ? 0 : 1, null, "-", null,
                        null, playInfo);
                return;
            case PlayConstant.LAUNCH_MODE_LIVE_SPORTS:
            case PlayConstant.LAUNCH_MODE_LIVE_WEISHI:
            case PlayConstant.LAUNCH_MODE_LIVE_OTHER:
            case PlayConstant.LAUNCH_MODE_LIVE_ENTERTAINMENT:
                DataStatistics.getInstance().sendLivePlayInfo25New(context, "0", "0",
                        DataConstant.StaticticsVersion2Constatnt.PlayerAction.LAUNCHER_ACTION, "0", "0", "-",
                        LetvUtils.getUID(), uuid, "-", aid, vid, "-", "-", "-", null, null, null, null, null, "-", LetvConfig
                                .getPcode(context),
                        PreferencesManager.getInstance().isLogin() ? 0 : 1, null, "-", null,
                        null, playInfo);
                return;
        }
        if (launchMode == PlayConstant.LAUNCH_MODE_DOWNLOAD) {// 下载
            DataStatistics.getInstance().sendPlayInfo24New(context, "0", "0",
                    DataConstant.StaticticsVersion2Constatnt.PlayerAction.LAUNCHER_ACTION, "0", "0", "-",
                    LetvUtils.getUID(), uuid, "-", aid, vid, null, null, "3", null, null, null, null, null, null, LetvConfig
                            .getPcode(context),
                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null, null, playInfo);
        } else if (launchMode == PlayConstant.LAUNCH_MODE_URI) {// 本地
            DataStatistics.getInstance().sendPlayInfo24New(context, "0", "0",
                    DataConstant.StaticticsVersion2Constatnt.PlayerAction.LAUNCHER_ACTION, "0", "0", "-",
                    LetvUtils.getUID(), uuid, "-", aid, vid, null, null, "4", null, null, null, null, null, null, LetvConfig
                            .getPcode(context),
                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null, null, playInfo);
        } else {
            DataStatistics.getInstance().sendPlayInfo24New(context, "0", "0",
                    DataConstant.StaticticsVersion2Constatnt.PlayerAction.LAUNCHER_ACTION, "0", "0", "-",
                    LetvUtils.getUID(), uuid, "-", aid, vid, null, null, "0", null, null, null, null, null, null, LetvConfig
                            .getPcode(context),
                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null, null, playInfo);
        }

        LogInfo.log("jc666", "launcher_action_from_baseplayactivityplayerlibs  uuid=" + uuid);
    }

    /**
     * 点播各接口缓冲时间统计
     *
     * @param mContext
     */
    @SuppressWarnings("deprecation")
    public static void staticticsLoadTimeInfo(Context context, AlbumPlayBaseFlow flow, ADPlayFragment adFragment) {
        AlbumPlayInfo playInfo = flow.getPlayInfo();
        try {
            flow.setFirstPlay(false);
            StringBuilder sb = new StringBuilder();
            // 广告拼接时长 add 5.8
            String adsUrls = playInfo.mAdUrl;
            sb.append("type1=" + DataUtils.getNetType(context) + "&");
            sb.append("type2=" + "0" + "&");

            if (adFragment != null && !playInfo.mIsAfterHomeClicked) {
                playInfo.mAdsRequestTime = adFragment.getAdsLoadingTime();
                if (playInfo.mAdCount > 0) {
                    playInfo.mAdsToalTime = adFragment.getAdsVideoTotalTime();
                    playInfo.mAdsJoinTime = adFragment.getAdsCombineCostTime();
                    playInfo.mAdsLoadConsumeTime = adFragment.getAdsPlayLoadTime();
                }
            } else if (playInfo.mIsAfterHomeClicked) {
                playInfo.mIsAfterHomeClicked = false;
            }
            // 请求接口时长，获取播放记录之后开始到接口返回的时间
            sb.append("type7=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.albumPayConsumeTime) + "&");
            // 获取广告拼接时长=>从调用地址接口到接口数据返回的时间(没广告时或者直播时为0)
            sb.append("type8=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.mAdsJoinTime) + "&");
            // 获取正式播放地址时长=>从请求调用真实播放开始到接口数据返回的时间（android）
            sb.append("type9=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.getRealUrlConsumeTime) + "&");
            // 汇总时长=>汇总时长是从点击视频海报到第一帧画面出现的时长（起播时长）
            // type10>type7+type9+type8(有广告时考虑)+type11+type13
            sb.append("type10=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.mTotalConsumeTime) + "&");
            // 广告接口请求时长=>从请求广告接口到接口返回解析数据完成时间
            sb.append("type11=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.mAdsRequestTime) + "&");
            // 广告总时长
            sb.append("type12=" + playInfo.mAdsToalTime + "&");
            // 视频加载时长=>拿到url播放地址到第一帧画面出现的时长(有广告是广告的加载时长，无广告是正片的加载时长)
            float loadingConsumeTimeTmp = StringUtils
                    .staticticsLoadTimeInfoFormat(playInfo.mAdCount > 0 ? playInfo.mAdsLoadConsumeTime
                            : playInfo.mVideoLoadConsumeTime);
            sb.append("type13=" + loadingConsumeTimeTmp + "&");
            // 正片加载时长=>拿到正片最终播放地址到出现正片第一帧画面的时长（Android端有）
            sb.append("type14=" + StringUtils.staticticsLoadTimeInfoFormat(playInfo.mVideoLoadConsumeTime));
            // 播放地址
            sb.append("&playurl=" + URLEncoder.encode(flow.getAlbumUrl().realUrl + ""));
            // 广告地址
            sb.append("&adurl=" + URLEncoder.encode(adsUrls + ""));
            sb.append("&time=").append(StringUtils.timeClockString("yyyyMMdd_HH:mm:ss"));
            sb.append("&ut=").append(playInfo.mTimeFromClickToPlayOnResume);
            sb.append("&pageid=" + StatisticsUtils.getPageId());
            DataStatistics.getInstance()
                          .sendActionInfo(context, "0", "0", LetvConfig.getPcode(context), "22", sb.toString(),
                    "0", flow.getCid() <= 0 ? "-" : flow.getCid() + "", flow.getAid() <= 0 ? "-" : flow.getAid() + "",
                    flow.getVid() <= 0 ? "-" : flow.getVid() + "", LetvUtils.getUID(), null, null, null, null,
                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null, "-");
            LogInfo.LogStatistics("点播-action 22:" + sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            StatisticsUtils.mClickImageForPlayTime = 0;
            playInfo.mTotalConsumeTime = 0;
            playInfo.mAdsRequestTime = 0;
            playInfo.mAdsToalTime = 0;
            playInfo.mAdsJoinTime = 0;
            playInfo.mAdsLoadConsumeTime = 0;
            playInfo.mVideoLoadConsumeTime = 0;
            playInfo.albumPayConsumeTime = 0;
            playInfo.getRealUrlConsumeTime = 0;
            playInfo.mAdCount = 0;
            playInfo.mAdsPlayFirstFrameTime = 0;
            playInfo.mIsPlayingAds = false;
        }
    }

    /**
     * 上报launch和init
     */
    public static void statisticsLaunchAndInit(Context context,AlbumPlayBaseFlow flow,boolean hasFinishAd) {
        LogInfo.LogStatistics("点播上报launch和init");
        if (hasFinishAd) {
            flow.getPlayInfo().mTotalConsumeTime =
                    StatisticsUtils.mClickImageForPlayTime == 0 ? System.currentTimeMillis()
                            : StatisticsUtils.mClickImageForPlayTime;
        }
        LogInfo.log("jc666", "起播起始时间：" +  flow.getPlayInfo().mTotalConsumeTime);
        if (TextUtils.isEmpty( flow.getPlayInfo().mUuid)) {
            flow.getPlayInfo().mUuid = LetvUtils.getUUID(context);
        }
        if (flow.getPlayInfo().mGlsbNum > 0) {
            flow.getPlayInfo().mUuidTimp = flow.getPlayInfo().mUuid + "_" + flow.getPlayInfo().mGlsbNum;
        } else {
            flow.getPlayInfo().mUuidTimp = flow.getPlayInfo().mUuid;
        }
        PlayStatisticsUtils.staticsLauncher(context,
                flow.getAid() + "",
                flow.getVid() + "", flow.getCid() + "", flow.getActivityLaunchMode(), flow.getPlayInfo().mUuidTimp, flow.getPlayInfo().mIpt);
        LogUtil.e("streamType" + flow.getStreamType() + ",realUrl=" + flow.getAlbumUrl().realUrl + ",totalTime=" +
                flow.getPlayInfo().totalTime);
        updatePlayDataStatistics(context,flow,DataConstant.StaticticsVersion2Constatnt.PlayerAction.INIT_ACTION, -1, false);
    }


    /**
     * 上报播放统计
     *
     * @param actionCode
     */
    public static void updatePlayDataStatistics(Context context,AlbumPlayBaseFlow flow,String actionCode, long pt, boolean isTopicPlay,
                                         Object... objects) {
        try {
            if (flow.getPlayInfo() == null) {
                LogInfo.LogStatistics("play info is null");
                return;
            }
            StringBuilder sb = getProperty(flow,isTopicPlay);
            sb.append("&replaytype=" + flow.getPlayInfo().mReplayType);
            if (!isTopicPlay) {
                sb.append("&adconfig=" + (AdsManager.getInstance().isShowAd() ? 1 : 0));
            }
            if (flow.getFrom() == PlayConstant.LAUNCH_FROM_RECOMMEND && flow.getVideoRecommend() != null &&
                    !isTopicPlay) {
                if (!TextUtils.isEmpty(flow.getVideoRecommend().getIs_rec()) &&
                        flow.getVideoRecommend().getIs_rec().equals("true")) {
                    sb.append("is_rec=1").append("&");
                } else {
                    sb.append("is_rec=0").append("&");
                }
            }
            sb.append("bf=" + flow.getPlayInfo().bufferNum + "&"); // 总的卡顿次数
            sb.append(
                    "bt=" + StringUtils.staticticsLoadTimeInfoFormat(flow.getPlayInfo().bufferTime * 1000) +
                            "&"); // 总的卡顿时间

            sb.append("bf1=" + flow.getPlayInfo().userBfCount + "&");// 拖拽卡顿次数
            sb.append("bt1=" +
                    StringUtils.staticticsLoadTimeInfoFormat(flow.getPlayInfo().userBfTimeTotal * 1000) +
                    "&");
            LogInfo.LogStatistics("userBfTimeTotal=" + flow.getPlayInfo().userBfTimeTotal);

            sb.append("bf2=" + flow.getPlayInfo().autofCount + "&");// 自然播放卡顿次数
            sb.append("bt2=" +
                    StringUtils.staticticsLoadTimeInfoFormat(flow.getPlayInfo().autoBfTimeTotal * 1000));

            if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.DRAG)) {
                if (objects != null && objects.length >= 2 && objects[0] instanceof Long &&
                        objects[1] instanceof Long) {
                    sb.append("&dr=" + StringUtils.time2StrBySec(((Long) objects[0]) / 1000) + "_" +
                            StringUtils.time2StrBySec(((Long) objects[1]) / 1000));
                }
            }

            if (flow.getPlayInfo().userBfCount > flow.getPlayInfo().userBfTimeTotal) {
                LogInfo.LogStatistics("*userBfCount > userBfTime*" + flow.getPlayInfo().userBfCount + "," +
                        flow.getPlayInfo().userBfTimeTotal);
            }
            LogInfo.LogStatistics("uType=" + StatisticsUtils.getStartType() + " ,ref=" +
                    StatisticsUtils.getPlayInfoRef());
            if (!isTopicPlay) {
                if (!TextUtils.isEmpty(StatisticsUtils.getStartType())) {
                    sb.append("&utype=" + StatisticsUtils.getStartType());
                } else if (flow.getFrom() == PlayConstant.LAUNCH_FROM_RECOMMEND) {// 大tip播放
                    StatisticsUtils.setPlayInfoRef(UIsUtils.isLandscape(context) ? 6 : 5);
                } else {// 其他
                    StatisticsUtils.setPlayInfoRef(-1);
                }
            } else {
                if (flow.getFrom() == PlayConstant.LAUNCH_FROM_PLAY_PAGE) {
                    StatisticsUtils.setPlayInfoRef(7);
                } else {
                    StatisticsUtils.setPlayInfoRef(-1);
                }
            }
            /**
             * add 5.8 统计增加参数
             * 1.atc 广告启播时长 play上报
             * 2.stc 影响广告起播的各阶段时长 各个时段上报 play上报
             * 3.prl 是否预加载 play上报
             * 4.cdev cde版本号 init上报
             * 5.caid cde为每个app指定的唯一ID init上报
             */
            long act = flow.getPlayInfo().mAdsPlayFirstFrameTime;
            // long stc = mPlayInfo.mAdsInteractiveTimeInFact;
            String actTime = null;
            // String stcTime = null;// 暂时不确定在5.8加不加
            if (act != 0) {
                actTime = StringUtils.staticticsLoadTimeInfoFormat(act) + "";
            }
            // if (stc != 0) {
            // stcTime = StringUtils.staticticsLoadTimeInfoFormat(stc) + "";
            // }
            int prl = 1;// 现在每个视频都有预加载，至少三秒
            String cdeVer = LetvSDKUtils.getCdeHelper(context).getServiceVersion();
            String cdeId = "3000";

            String ref = StatisticsUtils.getPlayInfoRef();
            StatisticsPlayInfo playInfo = new StatisticsPlayInfo();
            playInfo.setcTime(System.currentTimeMillis());
            playInfo.setIpt(flow.getPlayInfo().mIpt);

            String cid = flow.getCid() <= 0 ? "-" : flow.getCid() + "";
            String vid = flow.getVid() <= 0 ? "-" : flow.getVid() + "";

            if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.INIT_ACTION)) {
                if (flow.getLaunchMode() == PlayConstant.PLAY_DOWNLOAD && !isTopicPlay) {// 下载视频
                    // type=3
                    DataStatistics.getInstance()
                            .sendPlayInfoInit(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "3", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, cdeVer, cdeId, playInfo);
                } else if (flow.getLaunchMode() == PlayConstant.PLAY_DEFAULT && !isTopicPlay) {// 本地视频
                    // type=4
                    DataStatistics.getInstance()
                            .sendPlayInfoInit(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "4", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, cdeVer, cdeId, playInfo);
                } else {
                    DataStatistics.getInstance()
                            .sendPlayInfoInit(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "0", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null,
                                    flow.getZid() + "", cdeVer, cdeId, playInfo);
                }
            } else if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.PLAY_ACTION)) {
                LogInfo.LogStatistics(
                        "play。。。。act=" + StringUtils.staticticsLoadTimeInfoFormat(act) + " ,prl=" +
                                prl);
                int pay = 0;
                if (flow.getPlayingVideo() != null && flow.getPlayingVideo().getPay() == 1 &&
                        flow.getPlayInfo() != null) {
                    pay = flow.getPayInfo().getStatus() == 1 ? 2 : 1;
                }
                playInfo.setPay(pay);
                int joint = 0;
                if (flow.getAdCount() > 0) {
                    joint = flow.getAdCount() == 1 ? 2 : 1;
                }
                playInfo.setJoint(joint);

                if (flow.getLaunchMode() == PlayConstant.PLAY_DOWNLOAD && !isTopicPlay) {// 下载视频
                    // ty=3
                    DataStatistics.getInstance()
                            .sendPlayInfoPlay(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "3", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, actTime, "-", prl, playInfo);
                } else if (flow.getLaunchMode() == PlayConstant.PLAY_DEFAULT && !isTopicPlay) {// 本地视频
                    // type=4
                    DataStatistics.getInstance()
                            .sendPlayInfoPlay(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "4", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, actTime, "-", prl, playInfo);
                } else {
                    DataStatistics.getInstance()
                            .sendPlayInfoPlay(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "0", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null,
                                    flow.getZid() + "", actTime, "-", prl, playInfo);
                }
            } else {
                // 过滤time为0的情况
                if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.TIME_ACTION) &&
                        pt == 0) {
                    return;
                }
                if (flow.getLaunchMode() == PlayConstant.PLAY_DOWNLOAD && !isTopicPlay) {// 下载视频
                    // ty=3
                    DataStatistics.getInstance()
                            .sendPlayInfo24New(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "3", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, playInfo);
                } else if (flow.getLaunchMode() == PlayConstant.PLAY_DEFAULT && !isTopicPlay) {// 本地视频
                    // type=4
                    DataStatistics.getInstance()
                            .sendPlayInfo24New(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "4", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0
                                            : 1, null, null, playInfo);
                } else {
                    DataStatistics.getInstance()
                            .sendPlayInfo24New(context, "0", "0", actionCode, "0",
                                    (pt > 0 ? pt : 0) +
                                            "", "-", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp, cid,
                                    flow.getAid() <= 0 ? "-" : flow.getAid() + "", vid,
                                    flow.getPlayingVideo() == null ? null :
                                            flow.getPlayingVideo().getDuration() + "",
                                    flow.getPlayInfo().mRetryNum +
                                            "", "0", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, ref, sb.toString(), null, null, LetvConfig
                                            .getPcode(context),
                                    PreferencesManager.getInstance().isLogin() ? 0 : 1, null,
                                    flow.getZid() + "", playInfo);
                }
            }

            if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.END_ACTION)) {
                // resetTime();
                flow.getPlayInfo().mIpt = 0;
                flow.getPlayInfo().mUpdateCount = 0;
                flow.getPlayInfo().lastTimeElapsed = 0;
                flow.getPlayInfo().timeElapsed = 0;
                flow.getPlayInfo().bufferNum = 0;
                flow.getPlayInfo().bufferTime = 0;
                flow.getPlayInfo().autoBfTime = 0;
                flow.getPlayInfo().autoBfTimeTotal = 0;
                flow.getPlayInfo().userBfCount = 0;
                flow.getPlayInfo().userBfTimeTotal = 0;
                flow.getPlayInfo().mIsStaticsEnd = true;
            }

            if (actionCode.equals(DataConstant.StaticticsVersion2Constatnt.PlayerAction.PLAY_ACTION)) {
                flow.getPlayInfo().mIsStaticsEnd = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private  static StringBuilder getProperty(AlbumPlayBaseFlow flow,boolean isTopicPlay) throws Exception {
        StringBuilder sb = new StringBuilder();
        if (flow.getPlayInfo().mIsGslb) {
            sb.append("gslb=1" + "&");
        } else {
            sb.append("gslb=0" + "&");
        }
        if (flow.getPlayInfo().mIsCload) {
            sb.append("cload=1" + "&");
        } else {
            sb.append("cload=0" + "&");
        }
        if (LetvSDK.getInstance().isPush()) {
            sb.append("push=1" + "&");
            sb.append(
                    "type=" + (StatisticsUtils.isPushInfoHasPicture() ? "picture" : "word") + "&");
            String pushType =
                    (isTopicPlay ? LetvSDK.getPushType() : adjustPushTypeByLaunchMode(flow.getLaunchMode())) + "&";
            sb.append("pushtype=" + pushType);
        } else {
            sb.append("push=0" + "&");
        }
        sb.append(flow.getPlayInfo().mVideoSend + "&");
        sb.append(flow.getPlayInfo().mVformat + "&");
        sb.append("&time1=").append(DataUtils.getData(flow.getPlayInfo().userFirstBfTime));
        sb.append("&time2=").append(DataUtils.getData(flow.getPlayInfo().autoFirstBfTime));
        if (flow.isDownloadFile()) {
            sb.append("&offline=1");
        }
        // 5.7需求
        if (flow.getPlayingVideo() != null) {
            if (flow.getPlayingVideo().needPay()) {// 会员片
                sb.append("&pay=").append(PreferencesManager.getInstance().isVip() ? 1 : 0);
            } else {// 普通片
                sb.append("&pay=2");
            }
        }
        sb.append("&speed=" + StatisticsUtils.getSpeed());
        // 播放器数据上报
        if (StatisticsUtils.getType() != null) {
            sb.append("&player=" + StatisticsUtils.getType());// 播放器类型
        }
        sb.append("&sdk_ver=" + LetvMediaPlayerManager.getInstance().getSdkVersion());// sdk版本
        sb.append("&cpu=" + NativeInfos.getCPUClock());// 设置主频
        if ("ios".equals(LetvSDK.getInstance().getVideoFormat())) {
            sb.append("&cs=m3u8");// 码流类型
        } else if ("no".equals(LetvSDK.getInstance().getVideoFormat())) {
            sb.append("&cs=mp4");// 码流类型
        }
        sb.append("&su=" + (flow.getPlayInfo().mCurrentState == VideoViewH264m3u8.STATE_ERROR ? 0 : 1));

        return sb;
    }


    private static int adjustPushTypeByLaunchMode(int launchMode) {
        switch (launchMode) {
            case PlayConstant.LAUNCH_MODE_ALBUM:
                return 2; // 专辑
            case PlayConstant.LAUNCH_MODE_VIDEO:
                return 1; // 视频
            case PlayConstant.LAUNCH_MODE_TOPIC_ALBUM:
            case PlayConstant.LAUNCH_MODE_TOPIC_VIDEO:
                return 3; // 专题
            case PlayConstant.LAUNCH_MODE_LIVE_ENTERTAINMENT:
            case PlayConstant.LAUNCH_MODE_LIVE_OTHER:
            case PlayConstant.LAUNCH_MODE_LIVE_WEISHI:
            case PlayConstant.LAUNCH_MODE_LIVE_LUNBO:
            case PlayConstant.LAUNCH_MODE_LIVE_SPORTS:
            case PlayConstant.LAUNCH_MODE_LIVE_MUSIC:
                return 4; // 直播
            default:
                return 6; // 强唤醒
        }
    }


    /**
     * 上报卡顿统计(只为卡顿数据的上报)
     *
     * @param isStart
     * @param ut
     *         卡顿时长
     */
    public static void updateBlockDataStatistics(Context context,AlbumPlayBaseFlow flow,boolean isStart, long ut) {
        try {
            StringBuilder sb = getProperty(flow,false);
            // add 5.8 扩展字段增加卡顿时间点的上报
            sb.append("&time=" + flow.getPlayInfo().currTime / 1000);

            if (flow.getFrom() == PlayConstant.LAUNCH_FROM_PLAY_PAGE) {
                StatisticsUtils.setPlayInfoRef(7);
            } else {
                StatisticsUtils.setPlayInfoRef(-1);
            }
            StatisticsPlayInfo playInfo = new StatisticsPlayInfo();
            playInfo.setcTime(System.currentTimeMillis());
            playInfo.setIpt(flow.getPlayInfo().mIpt);
            String actionCode =
                    isStart ? DataConstant.StaticticsVersion2Constatnt.PlayerAction.BLOCK_ACTION
                            : DataConstant.StaticticsVersion2Constatnt.PlayerAction.BLOCK_END_ACTION;
            DataStatistics.getInstance()
                    .sendPlayInfo24New(context, "0", "0", actionCode, "0", "-",
                            (ut > 0 ? ut : 0) + "", LetvUtils.getUID(), flow.getPlayInfo().mUuidTimp,
                            flow.getCid() + "",
                            flow.getAid() + "",
                            flow.getVid() <= 0 ? "-" : flow.getVid() + "", flow.getPlayingVideo() == null ? null :
                                    flow.getPlayingVideo().getDuration() +
                                            "", flow.getPlayInfo().mRetryNum +
                                    "", "0", flow.getStreamLevel(), flow.getAlbumUrl().realUrl, StatisticsUtils.getPlayInfoRef(), sb
                                    .toString(), null, null, LetvConfig.getPcode(context),
                            PreferencesManager.getInstance().isLogin() ? 0 : 1, null,
                            flow.getZid() + "", playInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param mContext
     * @param fl
     *         分类
     * @param name
     *         名称
     * @param wz
     *         位置
     * @param id
     *         id
     * @category 动作统计上报
     */
    public static void staticticsErrorInfo(Context mContext,AlbumPlayBaseFlow flow, String fl, String name, int wz, int id) {
        try {
            StringBuilder sb = new StringBuilder();
            long aid = 0;
            long vid = 0;
            long cid = 0;
            if (flow.getPlayingVideo() != null) {
                aid = flow.getPlayingVideo().getPid();
                vid = flow.getPlayingVideo().getId();
                cid = flow.getPlayingVideo().getCid();
            }
            if (!TextUtils.isEmpty(fl)) {
                sb.append(
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_FL +
                                fl);
            }
            if (wz >= 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_WZ +
                        (wz + 1));
            }
            if (!TextUtils.isEmpty(name)) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_NA +
                        name);
            }
            if (id > 0) {
                sb.append("&" +
                        DataConstant.StaticticsVersion2Constatnt.StaticticsName.STATICTICS_NAM_CID +
                        id);
            }
            DataStatistics.getInstance()
                    .sendActionInfo(mContext, "0", "0", LetvConfig.getPcode(mContext), "20", sb
                                    .toString(), "0",
                            cid + "",
                            aid + "", vid + "", LetvUtils.getUID(), null, null, null, null,
                            PreferencesManager.getInstance().isLogin() ? 0 : 1, null);
            LogInfo.LogStatistics("albumplayflow: aid=" + aid + "_vid=" + vid + "_cid=" + cid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
